//
//  HHistoryExamViewController.m
//  NursingManages
//
//  Created by Zhen Huang on 17/9/29.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "HHistoryExamViewController.h"
#import "InitDbTableExamOnLine.h"
#import "HHistoryExamTableViewCell.h"
#import "Model_Hr_Exam_Primary.h"
#import "HHisExamContentViewController.h"
@interface HHistoryExamViewController ()
<UITableViewDelegate,
UITableViewDataSource>
@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, assign) NSInteger indexRows;
@property (nonatomic ,strong) NSIndexPath *indexPath;


@end

@implementation HHistoryExamViewController

- (instancetype)init {
    if (self) {
        
        self.dataArray = [NSMutableArray new];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataArray = [[InitDbTableExamOnLine alloc]getALLPrimary];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.tableview];
}

- (UITableView *)tableview
{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, NavBarHeight, ScreenWidth, ScreenHeight - NavBarHeight) style:UITableViewStylePlain];
        _tableview.dataSource = self;
        _tableview.delegate = self;
        _tableview.tableFooterView = [[UIView alloc]init];
        [_tableview registerClass:[HHistoryExamTableViewCell class] forCellReuseIdentifier:@"cell"];
    }
    return _tableview;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    Model_Hr_Exam_Primary *model = self.dataArray[indexPath.row];
    
    HHistoryExamTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.tempModel = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Model_Hr_Exam_Primary *model = self.dataArray[indexPath.row];
    HHisExamContentViewController *HExamVC = [[HHisExamContentViewController alloc]initWithDataArray:model.EMP_ID res_id:model.RES_ID result_flag:1 record_flag:1 type:1];
    HExamVC.title = model.title;
    [self.navigationController pushViewController:HExamVC animated:YES];
}
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"是否确定删除?"
                                                        message:nil
                                                       delegate:nil
                                              cancelButtonTitle:@"取消"
                                              otherButtonTitles:@"确定", nil];
        [alert show];
        alert.tag = 300;
        alert.delegate = self;
        self.indexRows = indexPath.row;
        self.indexPath = indexPath;
        
        
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (alertView.tag == 300) {
        switch (buttonIndex) {
            case 0: {
                return;
            }
                break;
            case 1: {
                Model_Hr_Exam_Primary *model = self.dataArray[self.indexRows];
                [self.dataArray removeObjectAtIndex:self.indexRows];
                InitDbTableExamOnLine *online = [[InitDbTableExamOnLine alloc]init];
                [online deleteTable:model.RES_ID];
                [online deleteDetail:model.RES_ID];
                [self.tableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:self.indexPath] withRowAnimation:UITableViewRowAnimationFade];

            }
                break;
            default:
                break;
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
